package com.jueling.culture.service.impl;


import com.jueling.culture.core.AbstractService;
import com.jueling.culture.core.service.TokenManageService;
import com.jueling.culture.dto.res.BackstageUserDto;
import com.jueling.culture.dto.res.EditBackstageUserDto;
import com.jueling.culture.exception.FrontShowException;
import com.jueling.culture.mapper.UserAuthorityMapper;
import com.jueling.culture.mapper.UserInfoMapper;
import com.jueling.culture.model.UserAuthority;
import com.jueling.culture.model.UserInfo;
import com.jueling.culture.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * @author linp
 * @date  2022/02/18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserInfoServiceImpl extends AbstractService<UserInfo> implements UserInfoService {

    @Resource
    @SuppressWarnings("unused")
    private UserInfoMapper userInfoMapper;
    @Resource
    private UserAuthorityMapper userAuthorityMapper;
    @Autowired
    private TokenManageService tokenManageService;

    /**
     * 用户登录
     *
     * @param username
     * @param md5Password
     * @return
     */
    @Override
    public UserInfo login(String username, String md5Password) {
        return userInfoMapper.login(username,md5Password);
    }

    /**
     * 查询用户列表
     *
     * @param keywords
     * @return
     */
    @Override
    public List<UserInfo> getUserList(String keywords) {
        return userInfoMapper.getUserList(keywords);
    }

    /**
     * 新增用户
     *
     * @param backstageUserDto
     */
    @Override
    public void addBackstageUser(BackstageUserDto backstageUserDto) {
        Integer selectCount= userInfoMapper.CheckUserName(backstageUserDto.getUserName());
        if(selectCount>0){
            throw new FrontShowException("用户名已存在");
        }
        //新增用户
        backstageUserDto.setCreateTime(new Date());
        backstageUserDto.setUpdateTime(backstageUserDto.getCreateTime());
        userInfoMapper.addBackstageUser(backstageUserDto);
        //新增用户权限
        List<UserAuthority> userAuthority=backstageUserDto.getUserAuthority();
        if(userAuthority!=null&&!userAuthority.isEmpty()){
            userAuthorityMapper.addUserAuthority(backstageUserDto.getUserId(),userAuthority);
        }
    }

    /**
     * 删除用户
     *
     * @param id
     */
    @Override
    public void deleteByUserId(String id) {
        UserInfo userInfo = findByIds(id).get(0);
        String userName = userInfo.getUserName();
        //删除用户权限
        userAuthorityMapper.deleteUserAuthority(id);
        //删除用户
        this.deleteByIds(id);
        //清除token
        tokenManageService.deleteTokenByUserId(userName);
    }

    /**
     * 编辑用户
     *
     * @param id
     * @param editBackstageUserDto
     */
    @Override
    public void updateBackstageUser(String id, EditBackstageUserDto editBackstageUserDto) {
        //删除用户权限
        userAuthorityMapper.deleteUserAuthority(id);
        //添加用户权限
        List<UserAuthority> userAuthority=editBackstageUserDto.getUserAuthority();
        if(userAuthority!=null&&!userAuthority.isEmpty()){
            userAuthorityMapper.addUserAuthority(id,userAuthority);
        }
        //修改用户信息
        userInfoMapper.updateBackstageUserInfo(id, editBackstageUserDto);
        //清除token
        UserInfo userInfo = findByIds(id).get(0);
        tokenManageService.deleteTokenByUserId(userInfo.getUserName());
    }

    /**
     * 查询登录用户的权限
     * @param userId
     * @return
     */
    @Override
    public List<UserAuthority> getUserAuthorityList(String userId) {
        return userAuthorityMapper.getUserAuthorityList(userId);
    }

    /**
     * 重置用户的密码
     * @param userId
     * @param password
     */
    @Override
    public void resetPassword(String userId, String password) {
        Integer count= userInfoMapper.resetPassword(userId,password);
        if(count==0){
            throw new FrontShowException("密码重置失败");
        }
    }

    /**
     * 判断用户是否存在(true表示不存在，false表示存在)
     * @param userName
     * @return
     */
    @Override
    public boolean checkUser(String userName) {
        boolean check;
        Integer selectCount= userInfoMapper.getBackstageUserCount(userName);
        check= selectCount == 0;
        return check;
    }
}
